export default {
  namespaced: true,
  state: {
    userInfo: {},
    token: '',
    permission_router: []
  },
  gutters: {},
  mutations: {
    setUserInfo( state, userInfo = {} ) {
      state.userInfo = userInfo;

      if ( userInfo && userInfo.permission_router ) {
        this.commit( 'user/setPermissionRouter' )
      }

      this.commit( 'user/setStorageUserInfo' );
    },
    setToken( state, info ) {
      state.token = info.text;
      this.commit( 'user/setStorageToken' )
    },
    setStorageUserInfo( state ) {
      try {
        uni.setStorageSync( 'userInfo', state.userInfo );
      } catch ( e ) {
        //TODO handle the exception
        console.log( 'set info err', e );
      }
    },
    setStorageToken( state ) {
      try {
        uni.setStorageSync( 'token', state.token )
      } catch ( err ) {
        //TODO handle the exception
        console.log( 'set token err', err );
      }
    },
    setPermissionRouter( state ) {
      try {
        uni.setStorageSync( 'permission_router', state.permission_router )
      } catch ( e ) {
        //TODO handle the exception
        console.log( 'set permission_router err', err );
      }
    },
    logoutFun( state ) {
      state.userInfo = {},
        state.token = '',
        state.permission_router = []
      this.commit( 'user/removeStorage' )
    },
    removeStorage() {
      uni.removeStorageSync( 'userInfo' )
      uni.removeStorageSync( 'token' )
      uni.removeStorageSync( 'permission_router' )
    }
  },
  actions: {}
}